package me.youline.dataServer.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import javax.transaction.Transactional;

import me.youline.dataServer.entity.MsgPraiseRecord;
import me.youline.dataServer.utils.IdPageSqlHelper;
import me.youline.dataServer.utils.IdPagination;

import org.perf4j.aop.Profiled;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

@Component
public class MsgPraiseRecordDAO {

	private static final String TABLE_NAME = "yline_social_praise_records";

	private static final String QUERY_PRAISE_USER_SQL = "select * from "
			+ TABLE_NAME + " where msgId = ? and %s order by id desc limit ?";


	private static final String INSERT_PRAISE_USER_SQL = "insert into "
			+ TABLE_NAME + "(msgId,praiseUserId) values (?,?)";

	private static final String DELETE_PRAISE_USER_SQL = "delete from "
			+ TABLE_NAME + " where msgId = ? and praiseUserId = ?";
	
	private static final String DELETE_PRAISE_RECORDS_SQL = " delete from "+TABLE_NAME+" where msgId = ? ";
	
	private static final String COUNT_PRAISE_USER_SQL = "select count(1) from "
			+ TABLE_NAME + " where msgId =? and praiseUserId = ?";
	
	private static final String COUNT_PRAISE_NUM_SQL = "select count(*) from "
			+ TABLE_NAME + " where msgId =? ";

	@Autowired
	private JdbcTemplate jdbcTemplate;

	private static final RowMapper<MsgPraiseRecord> ROW_MAPPER = new RowMapper<MsgPraiseRecord>() {

		@Override
		public MsgPraiseRecord mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			MsgPraiseRecord r = new MsgPraiseRecord();
			r.setId(rs.getLong("id"));
			r.setMsgId(rs.getLong("msgId"));
			r.setPraiseUserId(rs.getLong("praiseUserId"));
			return r;
		}
	};
	
	/**
	 * 根据原创消息ID删除点赞记录
	 * @param msgId
	 */
	public void deletePraiseRecordByMsgId(Long msgId){
		
		jdbcTemplate.update(DELETE_PRAISE_RECORDS_SQL, new Object[]{msgId});
	}

	/**
	 * 根据消息ID分页获取点赞记录
	 * 
	 * @param messageId
	 * @param page
	 * @return
	 */
	@Profiled(tag = "dao-根据消息ID分页获取点赞记录")
	public List<MsgPraiseRecord> getMsgPraiseRecordsByMsgId(Long messageId,
			IdPagination page) {
		IdPageSqlHelper helper = IdPageSqlHelper.create(page);
		String sql = String.format(QUERY_PRAISE_USER_SQL, helper.getPagingSqlCondition());
		Object[] data = null;
		if(helper.getIds().length == 0){
			data = new Object[]{messageId, page.getPageSize() };
		}else if(helper.getIds().length == 1){
			data = new Object[]{messageId, helper.getIds()[0], page.getPageSize() };
		}else if(helper.getIds().length == 2){
			data = new Object[]{messageId, helper.getIds()[0],helper.getIds()[1], page.getPageSize() };
		}
		List<MsgPraiseRecord> list = jdbcTemplate.query(sql, data,
					ROW_MAPPER);
		if (list.isEmpty()) {
			return Collections.emptyList();
		} else {
			return list;
		}
	}

	/**
	 * 增加消息点赞记录
	 * 
	 * @param messageId
	 * @param praiseUserId
	 */
	@Transactional
	@Profiled(tag = "dao-增加消息点赞记录")
	public void addMsgPraiseRecord(Long messageId, Long praiseUserId) {
		jdbcTemplate.update(INSERT_PRAISE_USER_SQL, new Object[] { messageId,
				praiseUserId });
	}

	/**
	 * 删除消息点赞记录
	 * 
	 * @param messageId
	 * @param praiseUserId
	 */
	@Transactional
	@Profiled(tag = "dao-删除消息点赞记录")
	public boolean deleteMsgPraiseRecord(Long messageId, Long praiseUserId) {
		int res = jdbcTemplate.update(DELETE_PRAISE_USER_SQL, new Object[] { messageId,
				praiseUserId });
		if(res > 0){
			return true;
		}
		return false;
	}

	/**
	 * 查询指定用户是否已经对该文章点赞
	 * 
	 * @param messageId
	 * @param praiseUserId
	 * @return
	 */
	@Profiled(tag = "dao-查询指定用户是否已经对该文章点赞")
	public boolean isExistPraiseRecord(Long messageId, Long praiseUserId) {
		Integer res = jdbcTemplate.queryForObject(COUNT_PRAISE_USER_SQL,
				new Object[] { messageId, praiseUserId }, Integer.class);
		if (res >= 1) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 统计消息点赞数
	 * @param messageId
	 * @return
	 */
	@Profiled(tag = "dao-统计消息点赞数")
	public int countMsgPraiseRecordNum(Long messageId){
		return jdbcTemplate.queryForObject(COUNT_PRAISE_NUM_SQL, new Object[]{messageId},Integer.class);
	}
	

}
